package com.ltmonitor.service.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ltmonitor.dao.IAlarmConfigDao;
import com.ltmonitor.dao.IBasicDataDao;
import com.ltmonitor.dao.impl.QueryResult;
import com.ltmonitor.entity.AlarmConfig;
import com.ltmonitor.service.IAlarmConfigService;
import com.ltmonitor.service.IDepartmentService;

/**
 * 报警配置服务
 * 
 * @author admin
 *
 */

@Service("alarmConfigService")
public class AlarmConfigService extends BaseService<Long, AlarmConfig>
		implements IAlarmConfigService {
	@Autowired
	private IAlarmConfigDao alarmConfigDao;
	@Autowired
	private IDepartmentService departmentService;

	@Transactional(readOnly = true)
	public Collection<AlarmConfig> init() {
		//String hql = "from AlarmConfig b  order by b.alarmSource ASC";
		Collection<AlarmConfig> ls = this.alarmConfigDao.loadAll();
		for (AlarmConfig a : ls) {
			String akey = a.getDepId() + "_" + a.getAlarmType() + "_"
					+ a.getAlarmSource();
			alarmConfigMap.put(akey, a);
			inheritMap.put(a.getDepId(), a.getDepId());
		}
		return ls;
	}

	/**
	 * 基本信息缓存
	 */
	public static HashMap<String, AlarmConfig> alarmConfigMap = new HashMap<String, AlarmConfig>();

	public static ConcurrentHashMap<Long, Long> inheritMap = new ConcurrentHashMap<Long, Long>();

	/**
	 * 获取某一组织机构下的报警配置
	 * 
	 * @param depId
	 * @return
	 */
	public List<AlarmConfig> getAlarmConfigByDepId(long depId) {
		String hql = "from AlarmConfig where depId = ? order by alarmSource";

		return this.alarmConfigDao.query(hql, depId);
	}

	public void deleteAlarmConfigByDepId(long depId) {
		String hql = "delete from AlarmConfig where depId = ?";

		this.alarmConfigDao.bulkUpdate(hql, new Object[] { depId });
	}

	@Override
	public void saveOrUpdateAll(Collection<AlarmConfig> alarmConfigs) {
		boolean isNew = true;
		this.baseDao.saveOrUpdateAll(alarmConfigs);
		long depId = 0;
		for (AlarmConfig a : alarmConfigs) {
			String key = a.getDepId() + "_" + a.getAlarmType() + "_"
					+ a.getAlarmSource();
			alarmConfigMap.put(key, a);
			depId = a.getDepId();
			isNew = a.getEntityId() == 0;
		}

		if (depId > 0 && isNew) {
			this.inheritMap.put(depId, depId);
			List<Long> depIdList = this.departmentService.getDepIdList(depId);
			depIdList.remove(depId);
			for (long childId : depIdList) {
				if (this.inheritMap.containsKey(childId)) {
					long v = this.inheritMap.get(childId);
					if (depIdList.contains(childId) == false)
						this.inheritMap.put(childId, depId);
				} else
					this.inheritMap.put(childId, depId);
			}
		}
	}

	/**
	 * 某一报警类型是否启用
	 * 
	 * @param alarmType
	 *            　报警类型
	 * @param alarmSource
	 *            　报警来源
	 * @return
	 */
	public boolean isAlarmEnabled(long depId, String alarmType,
			String alarmSource) {
		AlarmConfig ac = this.getAlarmConfig(depId, alarmType, alarmSource);
		return ac != null ? ac.isEnabled() : false;
	}

	public AlarmConfig getDefaultAlarmConfig(String alarmType,
			String alarmSource) {
		return this.getAlarmConfig(0, alarmType, alarmSource);
	}

	public AlarmConfig getAlarmConfig(long depId, String alarmType,
			String alarmSource) {

		if (inheritMap.containsKey(depId) == false) {
			List<Long> parentIdList = this.departmentService
					.getParentDepIdList(depId);
			parentIdList.add(0L);
			for (long parentId : parentIdList) {
				if (parentId != depId) {
					if (this.inheritMap.containsKey(parentId)) {
						String key = parentId + "_" + alarmType + "_"
								+ alarmSource;
						this.inheritMap.put(depId, parentId);
						return alarmConfigMap.get(key);
					}
				}
			}
			return null;
		} else {
			long id = inheritMap.get(depId);
			String key = id + "_" + alarmType + "_" + alarmSource;
			return alarmConfigMap.get(key);
		}
	}

}
